package com.study.project.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.project.dao.EmployeeMapper;
import com.study.project.entity.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class EmployeeService {

    private  static final Logger logger = LoggerFactory.getLogger(EmployeeService.class);

    @Autowired
    private EmployeeMapper employeeMapper;

    public Employee save(Employee employee){
        employeeMapper.insert(employee);
        logger.error("cuowu");
        return employee;
    }

    public void delete(String id){
        employeeMapper.deleteByPrimaryKey(id);
    }

    public Employee update(Employee employee){
        int i = employeeMapper.updateByPrimaryKey(employee);
        if(i == 1) {
            System.out.println("update");
        }
        return employee;
    }

    public Employee select(String id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    public Object findAll() {
        PageHelper.startPage(1, 5, true);
        List<Employee> list = employeeMapper.findAll();
        PageInfo<Employee> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}